﻿using ExamCommon;
using ExamServerViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExamServerBLL
{
    public class SettingService
    {
        // 配置信息
        private static ServerConfiguration configuration = null;

        // 构造函数
        public SettingService()
        {
            configuration = GetConfiguration();
        }

        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <returns></returns>
        public static ServerConfiguration GetConfiguration()
        {   // 读取配置文件
            configuration = ServerConfiguration.ReadConfiguration();
            return configuration;
        }


        /// <summary>
        /// 启动socket服务
        /// </summary>
        /// <returns></returns>
        public bool StartSocketServer()
        {
            if (configuration == null)
            {
                return false;
            }
            bool flag = ServerSocketService.CreateServerTCPSocket(configuration.SocketPort);
            return flag;
        }

        /// <summary>
        /// 停止socket服务
        /// </summary>
        public void StopSocketServer()
        {
            if (configuration == null)
            {
                return;
            }
            ServerSocketService.CloseServerTCPSocket();
        }


        /// <summary>
        /// 获取初始配置信息
        /// </summary>
        /// <returns></returns>
        public SettingServiceViewModel GetSettingInfo()
        {
            if (configuration == null)
            {
                return null;
            }
            SettingServiceViewModel viewModel = new SettingServiceViewModel();
            // ip地址
            viewModel.AllIpAddress = NetHelper.GetAllIP();
            viewModel.IpAddress = NetHelper.GetPhysicalIP();
            // 端口号
            viewModel.SocketPort = configuration.SocketPort.ToString();
            // 状态显示
            viewModel.IsShowIpAndPort = configuration.IsShowIpAndPort;
            viewModel.IsShowAllIp = configuration.IsShowAllIp;
            viewModel.IsShowOnlineNum = configuration.IsShowOnlineNum;
            viewModel.IsShowExamNum = configuration.IsShowExamNum;
            viewModel.IsShowOnlineBubble = configuration.IsShowOnlineBubble;
            viewModel.IsShowTime = configuration.IsShowTime;
            viewModel.TimePattern = configuration.TimePattern;
            // 数据库连接信息
            if (configuration.SQLConnectInfo == null)
            {
                viewModel.SqlHost = string.Empty;
                viewModel.SqlPort = string.Empty;
                viewModel.SqlUsername = string.Empty;
                viewModel.SqlPassword = string.Empty;
            }
            else
            {
                viewModel.SqlHost = configuration.SQLConnectInfo.MysqlHost;
                viewModel.SqlPort = configuration.SQLConnectInfo.MysqlPort.ToString();
                viewModel.SqlUsername = configuration.SQLConnectInfo.Username;
                viewModel.SqlPassword = configuration.SQLConnectInfo.Password;
            }
            return viewModel;
        }

        /// <summary>
        /// 获取socket服务的状态
        /// </summary>
        /// <returns></returns>
        public bool GetSocketStatus()
        {
            return ServerSocketService.GetSocketStatus(configuration.SocketPort);
        }

        /// <summary>
        /// 写配置文件
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public bool WriteConfig(ServerConfiguration config)
        {
            return ServerConfiguration.WriteConfiguration(config);
        }
    }
}
